/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package radio_GUI;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.table.DefaultTableModel;
import model.Album;
import model.DBManager;
import model.Song;


/**
 *
 * @author ΗΛΕΚΤΡΟΛΟΓΟΙ
 */
public class Edit_New_AlbumArtist extends javax.swing.JFrame {

    /**
     * Creates new form Edit_New_AlbumArtist
     */
     
    
    public Edit_New_AlbumArtist() {       
        this.em = DBManager.getEm();// O em
        this.aa = new model.AlbumArtist(); //Καινουριο Album οποτε το POJO album_artist είναι null
        this.al = new model.Album();
        album = new Album(); //Καινουριο album  οποτε κενουριο αντικειμενο
        albumart = new model.AlbumArtist();
        initComponents();

       
        fillSongList(); //Γεμιζουμε το jTable1
        //album = album1;// H album περνει τις τιμες της album1=> this.al
                       //                                     => παραμετρου
    }

    public Edit_New_AlbumArtist(model.AlbumArtist aa,boolean formType) {
        this.formType = formType;// Αν είναι φορμα μεταβολής θεσε την bool true
        if (!formType){ //Αν είναι delete form θεσε την bool σε true
            delForm=true;
        }
        this.aa = aa;// Υπάρχει καταχόριση στο album_artist
        this.al = aa.getAlbumId();// Υπάρχει album να δουλέψουμε
        
        
        this.em = DBManager.getEm();////em
        ///////////////////////////////////////
        
        slToAdd = aa.getAlbumId().getSongList();//Η λιστα τραγουδιων του jTable1
        
   
        initComponents();
   
        album = album1;// H album περνει τις τιμες της album1=> this.al
                       //                                     => παραμετρου
        albumart = aa1;//H album art περνει τιμες μεσω της this.aa απο το GUI
        //Επέλεξε τις καταλληλες γραμμες στα combobox
        ComboArtist.setSelectedItem(aa.getArtistId());
        jComboBox1.setSelectedItem(aa.getAlbumId().getMpcId());
        jComboBox3.setSelectedItem(aa.getAlbumId().getType());
        
        if(delForm){ //Παραμετροποιουμε την φορμα για διαγραφή
            //TODO απενεργοποιηση των κουμπιων που δεν χρειαζομαστε....
            jButtonAction.setText("Διαγραφή");
            //jButtonInseSong.setEnabled(false);
            //jButtonDelSong.setEnabled(false);
            //jTextField2.setEnabled(false);
        }
        
       
        fillSongList();//Γεμιζουμε το jTable1    
    }
    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        album1 = al;
        aa1 = this.aa;
        song1 = new model.Song();
        entityManager1 = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("radioPU").createEntityManager();
        query1 = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT s FROM Song s");
        list1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : query1.getResultList();
        artist1 = new model.Artist();
        radioPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("radioPU").createEntityManager();
        entityManager2 = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("radioPU").createEntityManager();
        combo_Artist = java.beans.Beans.isDesignTime() ? null : entityManager2.createQuery("SELECT a FROM Artist a");
        list2 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : combo_Artist.getResultList();
        musicproductioncompanyQuery = java.beans.Beans.isDesignTime() ? null : radioPUEntityManager.createQuery("SELECT m FROM Musicproductioncompany m");
        musicproductioncompanyList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : musicproductioncompanyQuery.getResultList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        ComboArtist = new javax.swing.JComboBox<model.Artist>();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jButtonAction = new javax.swing.JButton();
        jButtonDelSong = new javax.swing.JButton();
        jButtonInseSong = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        DatePicker1 = new org.jdesktop.swingx.JXDatePicker();
        jComboBox3 = new javax.swing.JComboBox();
        jTextField2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("ΔΙΑΧΕΙΡΙΣΗ ΑΛΜΠΟΥΜ");

        jLabel2.setText("ΤΙΤΛΟΣ:");

        jLabel5.setText("Λίστα Τραγουδιών:");

        jLabel6.setText("ΤΥΠΟΣ:");

        jLabel7.setText("Αριθμος Άλμπουμ:");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, aa1, org.jdesktop.beansbinding.ELProperty.create("${albumId.disknumber}"), jTextField3, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jLabel8.setText("Εταιρεία Παρ.:");

        jLabel9.setText("Καλλιτέχνης:");

        jLabel10.setText("Ημ. Κυκλοφορίας:");

        jComboBox1.setRenderer( new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof model.Musicproductioncompany) {
                    model.Musicproductioncompany mec = (model.Musicproductioncompany)value;
                    setText(mec.getName());
                }
                return this;
            }
        });

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicproductioncompanyList, jComboBox1);
        bindingGroup.addBinding(jComboBoxBinding);

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        ComboArtist.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof model.Artist) {
                    model.Artist mec = (model.Artist)value;
                    setText(mec.getArtisticname());
                }
                return this;
            }
        });

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list2, ComboArtist);
        bindingGroup.addBinding(jComboBoxBinding);

        ComboArtist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ComboArtistActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Τίτλος", "Διαρκεια", "Αριθμός Track"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        jButtonAction.setText("Αποθήκευση");
        jButtonAction.setToolTipText("");
        jButtonAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonActionActionPerformed(evt);
            }
        });

        jButtonDelSong.setText("Διαγραφή");
        jButtonDelSong.setToolTipText("");
        jButtonDelSong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDelSongActionPerformed(evt);
            }
        });

        jButtonInseSong.setText("Εισαγωγή");
        jButtonInseSong.setToolTipText("");
        jButtonInseSong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonInseSongActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Ακύρωση");
        jButtonCancel.setToolTipText("");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, aa1, org.jdesktop.beansbinding.ELProperty.create("${albumId.releasedate}"), DatePicker1, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);

        DatePicker1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DatePicker1ActionPerformed(evt);
            }
        });

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CD", "LP", "MP" }));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, aa1, org.jdesktop.beansbinding.ELProperty.create("${albumId.title}"), jTextField2, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonInseSong)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonDelSong))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 732, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 722, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(321, 321, 321)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel9)
                        .addGap(10, 10, 10)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jComboBox1, 0, 80, Short.MAX_VALUE)
                    .addComponent(ComboArtist, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTextField2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jComboBox3, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(DatePicker1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)))
                .addGap(55, 55, 55))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonAction)
                .addGap(64, 64, 64)
                .addComponent(jButtonCancel)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator3)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(321, 321, 321)
                .addComponent(jLabel5)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {ComboArtist, jComboBox1, jTextField3});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel8, jLabel9});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButtonAction, jButtonCancel, jButtonDelSong, jButtonInseSong});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1)
                .addGap(9, 9, 9)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel8)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DatePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(ComboArtist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonDelSong)
                    .addComponent(jButtonInseSong))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCancel)
                    .addComponent(jButtonAction))
                .addGap(40, 40, 40))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jButtonActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonActionActionPerformed
        // TODO add your handling code here:
        ///////////////////////////////////////////////////////////////////
        
        
 
        //Δινουμε τιμες στο album απο την φορμα
        album.setTitle(jTextField2.getText());
        album.setDisknumber(Integer.parseInt(jTextField3.getText()));
        album.setType((String) jComboBox3.getSelectedItem());
        album.setReleasedate(DatePicker1.getDate());
       
        ////seting mpc
        model.Musicproductioncompany testMPCOBJ 
                                    = new model.Musicproductioncompany();
        testMPCOBJ = 
                musicproductioncompanyList.get(jComboBox1.getSelectedIndex());
        album.setMpcId(testMPCOBJ);
        
        album.setSongList(slToAdd);
        
        //Δινουμε τιμες στο album_artist
        //albumart = new model.AlbumArtist();
        albumart.setAlbumId(album);
        albumart.setArtistId(list2.get(ComboArtist.getSelectedIndex()));
                
        
        //Καταχορουμε με τον em τισ τιμες της φορμας αναλογα με τον τύπο της
        if (formType){//Φορμα Μεταβολής στοιχείων
            em.getTransaction().begin();          
            em.merge(album);
            em.getTransaction().commit();
            em.getTransaction().begin();          
            em.merge(albumart);
            em.getTransaction().commit();
        }else if (delForm){//Φορμα διαγραφής
            try{
            //em.getTransaction().begin();
            for (Song sFL : slToAdd){
                em.getTransaction().begin();
                Song s = em.merge(sFL);
                em.remove(s);
                em.getTransaction().commit();
            }
            em.getTransaction().begin();
            model.AlbumArtist aatd = em.merge(albumart);
            em.remove(aatd);
            em.getTransaction().commit();
            
            em.getTransaction().begin();
            Album atd = em.merge(album);
            em.remove(atd);
            em.getTransaction().commit();

            }catch (Exception e){System.out.println("MArtirao: "+ e );}
            
        }else{//Φορμα εισαγωγής νεου album
        
            for (Song sFL : slToAdd){
                em.getTransaction().begin();
                em.refresh(sFL);
                em.getTransaction().commit();
            }
            em.getTransaction().begin();          
            em.merge(album);
            em.getTransaction().commit();
            em.getTransaction().begin();          
            em.persist(albumart);
            em.getTransaction().commit();
        }
        
        this.dispose();//Dispose την φορμα που δουλεβουμε
        new AlbumArtistForm().setVisible(true);//Νεα AlbumArtistForm
    }//GEN-LAST:event_jButtonActionActionPerformed

    
    
    
    private void jButtonDelSongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDelSongActionPerformed
        

        //για να διαγράψουμε ενα τραγουδι απο το album περνουμε την επιλεγεμενη
        //γραμμη (το τραγουδι δηλαδη) το αφερούμε με em.remove , το αφερούμε
        // απο την λιστα με τα τραγουδια απο το album που επεξεργαζομαστε και
        //τελος ενιμερονουμε το jtable(τον πινακα)
        int i = jTable1.convertRowIndexToModel(jTable1.getSelectedRow());
        em.getTransaction().begin();
        Song s = em.merge(slToAdd.get(i));
        em.remove(s);
        em.getTransaction().commit();
        slToAdd.remove(i);
        fillSongList();
        
    }//GEN-LAST:event_jButtonDelSongActionPerformed

    private void jButtonInseSongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInseSongActionPerformed
        //περνουμε τιμες απο το model του jtable1 (τα δεδομενα του δηλαδη)
        //τις βαζουμε σε ενα αντικειμενο song και τις κανουμε persist me em
        //το προσθετουμε στη λιστα με τα τραγουδια του album και τελος 
        //δημιουργούμε μια νεα γραμμη για insert song
        modelSongsList = (DefaultTableModel) jTable1.getModel();
      
        int noOfSongs= modelSongsList.getRowCount();
        int i = noOfSongs-1 ; 
        Integer songDu=0 , songNu = 0;
        String songTi="";
        boolean valuesNull = false;
        model.Song sToAdd = new model.Song();
        try{
            songDu =(Integer)
                            (modelSongsList.getValueAt( i,  1 ));
            songNu =(Integer)(modelSongsList.getValueAt( i,  2 ));
            songTi = modelSongsList.getValueAt( i,  0 ).toString();
            System.out.println(i);
        }catch (Exception e){
            System.out.println(e+"Null values!.!>!!>!");
            valuesNull = true;
        }
        if (!valuesNull){
                    
            System.out.println(album);
            sToAdd.setTitle(songTi);
            System.out.println(songDu +" "+ songNu +""+songTi);
            sToAdd.setDuration(songDu);
            sToAdd.setTracknr(songNu);
 
            if(i==0 && !formType  && !delForm){
                this.album.setTitle(jTextField2.getText());
                album.setType((String) jComboBox3.getSelectedItem());
                model.Musicproductioncompany testMPCOBJ 
                                    = new model.Musicproductioncompany();
                testMPCOBJ = 
                musicproductioncompanyList.get(jComboBox1.getSelectedIndex());
                album.setMpcId(testMPCOBJ);
                em.getTransaction().begin();
                em.persist(this.album);
                em.getTransaction().commit();
                //em.persist(this.aa);
                System.out.println(album);
            }
            sToAdd.setAlbumId(album);
            em.getTransaction().begin();
            em.persist(sToAdd);
            em.getTransaction().commit();
            System.out.println("SOng added to db");
            slToAdd.add(sToAdd);
            Object[] blankRow ={null,null,null};
            modelSongsList.addRow(blankRow);
        }
                
            
        System.out.println("NEW ROw added?");
     
        
        
    }//GEN-LAST:event_jButtonInseSongActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // TODO add your handling code here:
        
         //  this.dispose(); //P.A. "katastrepse" playlist Jframe
        new AlbumArtistForm().setVisible(true);
        this.dispose(); //P.A. "katastrepse" playlist Jframe
    
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void ComboArtistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComboArtistActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ComboArtistActionPerformed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void DatePicker1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DatePicker1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_DatePicker1ActionPerformed
    private void fillSongList(){
        //Γεμιζουμε το Jtable1
        modelSongsList =( DefaultTableModel) jTable1.getModel();
        modelSongsList.setRowCount(0);//Αδιασμα model (Αρχικοποιηση)
        List<model.Song> sl = new ArrayList<model.Song>();
     //   if (aa1!= null){
       //     sl = aa1.getAlbumId().getSongList();
      //  }
        sl = this.slToAdd;
        
        System.out.println(sl);
        for (model.Song sle : sl){
            Object[] rowToAdd = {sle.getTitle(),
                                sle.getDuration(),sle.getTracknr()};
            modelSongsList.addRow(rowToAdd);
            System.out.println(sle.getTitle());
        }
        //Προσθετουμε μια κενη γραμμη για insert song
        Object[] blankRow ={null,null,null};
        modelSongsList.addRow(blankRow);
        //jTable1.setModel(modelSongsList);
        //modelSongsList.fireTableRowsInserted(0, modelSongsList.getRowCount());
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Edit_New_AlbumArtist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Edit_New_AlbumArtist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Edit_New_AlbumArtist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Edit_New_AlbumArtist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DBManager.openConnection();//Ksekina ton entity manager gia test                
                new Edit_New_AlbumArtist().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox<model.Artist> ComboArtist;
    private org.jdesktop.swingx.JXDatePicker DatePicker1;
    private model.AlbumArtist aa1;
    private model.Album album1;
    private model.Artist artist1;
    private javax.persistence.Query combo_Artist;
    private javax.persistence.EntityManager entityManager1;
    private javax.persistence.EntityManager entityManager2;
    private javax.swing.JButton jButtonAction;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonDelSong;
    private javax.swing.JButton jButtonInseSong;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private java.util.List<model.Song> list1;
    private java.util.List<model.Artist> list2;
    private java.util.List<model.Musicproductioncompany> musicproductioncompanyList;
    private javax.persistence.Query musicproductioncompanyQuery;
    private javax.persistence.Query query1;
    private javax.persistence.EntityManager radioPUEntityManager;
    private model.Song song1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
private model.Album al;//Antikeimeno gia bind GUI
private model.Album album;//Το αντικειμενο που θα δουλεψουμε με τον em
EntityManager em;
private model.AlbumArtist aa;//Antikeimeno gia bind GUI
model.AlbumArtist albumart;//Το αντικειμενο που θα δουλεψουμε με τον em
private DefaultTableModel modelSongsList = new DefaultTableModel();//Το 
                                                        //model(data)του jTable1
private List<model.Song> slToAdd = new ArrayList<model.Song>();//Λιστα με 
                                                    //τα τραγουδια του album
private boolean formType = false; //Boolean φορμα μεταβολης του αντικειμένου album
private boolean delForm = false;//Boolean φορμα διαγραφης
}
